﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;

namespace BrokerBase.Lib.Client
{
    public class TerminalInfo
    {
        public static TerminalInfo Parse(byte[] data)
        {
            TerminalInfo info = new TerminalInfo();

            string xml = Encoding.Unicode.GetString(data);

            TextReader reader = new StringReader(xml);

            XElement element = XElement.Load(reader);

            info.DISKSerialNo = element.Element("DISkSerialNo").Value;
            info.MACSerialNo = element.Element("MACSerialNo").Value;
            info.MBSerialNo = element.Element("MBSerialNo").Value;
            info.OSVersion = element.Element("OSVersion").Value;
            info.ClientTime = DateTime.Parse(element.Element("ClientTime").Value);

            return info;
        }

        public string DISKSerialNo { get; set; }

        public string MACSerialNo { get; set; }
        /// <summary>
        /// 主板序列号
        /// </summary>
        public string MBSerialNo { get; set; }

        public DateTime ClientTime { get; set; }

        public string OSVersion { get; set; }

        public XElement GetXml()
        {

            XElement diskSerialNo = new XElement("DISkSerialNo", DISKSerialNo);

            XElement macSerialNo = new XElement("MACSerialNo", MACSerialNo);

            XElement mbSerialNo = new XElement("MBSerialNo", MBSerialNo);

            XElement clientTime = new XElement("ClientTime", ClientTime.ToString("yyyy-MM-dd"));

            XElement osVersion = new XElement("OSVersion", OSVersion);

            XElement root = new XElement("TerminalInfo", diskSerialNo, macSerialNo, mbSerialNo, clientTime, osVersion);

            return root;
        }

        public byte[] GetData()
        {
            XElement xml = GetXml();

            string text = xml.ToString();

            byte[] data = Encoding.Unicode.GetBytes(text);

            return data;

        }
    }
}
